{% extends "base.html" %}

{% block title %}邮箱验证 - {{ super() }}{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-md-6">
        <div class="card shadow-sm">
            <div class="card-header bg-info text-white">
                <h4 class="mb-0">邮箱验证</h4>
            </div>
            <div class="card-body">
                <form method="POST">
                    {{ form.hidden_tag() }}
                    <div class="mb-3">
                        {{ form.email.label(class="form-label") }}
                        <div class="input-group">
                            {{ form.email(class="form-control") }}
                            <button type="button" class="btn btn-outline-primary" id="sendCaptchaBtn">发送验证码</button>
                        </div>
                        {% for error in form.email.errors %}
                            <div class="text-danger">{{ error }}</div>
                        {% endfor %}
                    </div>
                    <div class="mb-3">
                        {{ form.captcha.label(class="form-label") }}
                        {{ form.captcha(class="form-control") }}
                        {% for error in form.captcha.errors %}
                            <div class="text-danger">{{ error }}</div>
                        {% endfor %}
                    </div>
                    <div class="d-grid gap-2">
                        {{ form.submit(class="btn btn-info") }}
                    </div>
                </form>
            </div>
            <div class="card-footer text-center">
                <p class="mb-0">验证成功后即可 <a href="{{ url_for('auth_bp_zc.login') }}">登录</a></p>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
document.getElementById('sendCaptchaBtn').addEventListener('click', function() {
    const email = document.querySelector('input[name="email"]').value;
    if (!email) {
        alert('请先输入邮箱地址');
        return;
    }
    
    this.disabled = true;
    fetch('{{ url_for("auth_bp_zc.send_captcha") }}', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: `email=${encodeURIComponent(email)}`
    })
    .then(response => response.json())
    .then(data => {
        if (data.status === 'success') {
            alert('验证码已发送到您的邮箱');
            let countdown = 60;
            const timer = setInterval(() => {
                this.textContent = `${countdown}秒后重试`;
                countdown--;
                if (countdown < 0) {
                    clearInterval(timer);
                    this.disabled = false;
                    this.textContent = '发送验证码';
                }
            }, 1000);
        } else {
            alert(data.message || '发送失败，请重试');
            this.disabled = false;
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('发送失败，请重试');
        this.disabled = false;
    });
});
</script>
{% endblock %} 